﻿package com.tudou.home.model.ad.base
{
    import com.tudou.ad.consts.*;
    import com.tudou.events.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.view.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import com.tudou.v2.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.system.*;

    public class AdVideoProxyBase extends AdProxyBase
    {
        protected var loadedV2:Boolean = false;
        protected var loaderBgTime:int = 0;
        protected var loadedBg:Boolean = false;
        protected var v2Loader:V2AdLoader;
        protected var loaderBg:TdLoader;
        protected var _loaderV2Time:int = 0;

        public function AdVideoProxyBase(param1:String)
        {
            super(param1);
            return;
        }// end function

        protected function startViewFronts() : void
        {
            if (!view)
            {
                return;
            }
            view.mainWidth = AdSize.FRONTBACK_VIDEO_WIDTH;
            view.mainHeight = AdSize.FRONTBACK_VIDEO_HEIGHT;
            view.bgWidth = AdSize.FRONTBACK_BACKGROUND_WIDTH;
            view.bgHeight = AdSize.FRONTBACK_BACKGROUND_HEIGTH;
            view.mainTop = PlayerConsts.VIDEO_TOP;
            view.adItemInfo = adItemInfo;
            view.adThrowInfo = adThrowInfo;
            view.countDownTime = getRemainDt();
            view.isExpand = PlayerConsts.expandStage;
            view.tracker = _tracker;
            view.setStuff(frontStuff);
            view.initToShow();
            createViewListeners();
            var _loc_1:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            var _loc_2:* = _loc_1.isFullScreen ? (PlayerConsts.FULLSCREEN_RECT) : (PlayerConsts.NORMALSCREEN_RECT);
            goTransform(_loc_2.width, _loc_2.height, _loc_1.isFullScreen);
            view.startAd();
            return;
        }// end function

        protected function bgStuffTimeout() : void
        {
            sendTrack(NodeLog.AD_BG_STUFF_REQUEST_TIMEOUT, [loaderBg.bytesLoaded, loaderBg.bytesTotal]);
            return;
        }// end function

        protected function startLoadV2(param1:int) : void
        {
            if (v2Loader)
            {
                adV2Start();
                v2Loader.load(new AdBaseInfo(param1, adItemInfo), "");
            }
            return;
        }// end function

        protected function ioErrorBgHlr(event:LoaderEvent) : void
        {
            sendTrack(NodeLog.AD_BG_STUFF_REQUEST_ERROR);
            return;
        }// end function

        protected function bgStuffComplete() : void
        {
            if (!view)
            {
                return;
            }
            view.setBgStuff(bgStuff);
            view.addToShowBgStuff();
            return;
        }// end function

        protected function loadV2() : void
        {
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            if (!adItemInfo || !adItemInfo.videoUrl)
            {
                adV2End();
                return;
            }
            var _loc_1:* = adItemInfo.videoUrl;
            if (_loc_1.indexOf("http://") != -1)
            {
                if (adItemInfo.isVideo)
                {
                    _loc_2 = _loc_1.split(",");
                    _loc_3 = [];
                    _loc_4 = 0;
                    _loc_5 = _loc_2.length;
                    while (_loc_4 < _loc_5)
                    {
                        
                        _loc_1 = URLUtil.addKeyValue(_loc_2[_loc_4], "igrft", RandString.noCache);
                        _loc_3.push(_loc_1);
                        _loc_4++;
                    }
                    adItemInfo.videoUrl = _loc_3.join(",");
                }
                adV2End();
            }
            else
            {
                _loc_6 = _loc_1.split("&");
                _loc_7 = Number(_loc_6[0]);
                if (isNaN(_loc_7) || _loc_7 <= 0)
                {
                    adV2End();
                    return;
                }
                initV2();
                startLoadV2(_loc_7);
            }
            return;
        }// end function

        override protected function get frontStuff() : DisplayObject
        {
            if (adItemInfo && adItemInfo.isVideo)
            {
                return new Shape();
            }
            return loader ? (loader.content) : (null);
        }// end function

        protected function v2LoadedHlr(event:V2LoaderEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            adV2Complete();
            var _loc_2:* = v2Loader.getVideoInfo();
            adItemInfo.videoUrl = "";
            if (_loc_2 && _loc_2.v2ErrorNo == 0)
            {
                _loc_3 = _loc_2.getAllNodeUrl();
                _loc_4 = [];
                _loc_6 = 0;
                _loc_7 = _loc_3.length;
                while (_loc_6 < _loc_7)
                {
                    
                    _loc_5 = URLUtil.addKeyValue(_loc_3[_loc_6], "vidtp", "1");
                    _loc_5 = URLUtil.addKeyValue(_loc_5, "igrft", RandString.noCache);
                    _loc_4.push(_loc_5);
                    _loc_6++;
                }
                adItemInfo.videoUrl = _loc_4.join(",");
            }
            adV2End();
            return;
        }// end function

        protected function endBgHlr(event:LoaderEvent) : void
        {
            if (loadedBg)
            {
                bgStuffComplete();
            }
            else
            {
                bgStuffError();
            }
            return;
        }// end function

        protected function adV2Error() : void
        {
            sendTrack(NodeLog.AD_V2_REQUEST_ERROR);
            return;
        }// end function

        protected function set v2Timeout(param1:int) : void
        {
            _loaderV2Time = param1;
            return;
        }// end function

        protected function loadBg() : void
        {
            if (adItemInfo && adItemInfo.backUrl)
            {
                initBg();
                startLoadBg(adItemInfo.backUrl);
            }
            return;
        }// end function

        protected function adV2Complete() : void
        {
            sendTrack(NodeLog.AD_V2_REQUEST_COMPLETE);
            return;
        }// end function

        protected function bgStuffError() : void
        {
            return;
        }// end function

        protected function timeoutErrorBgHlr(event:LoaderEvent) : void
        {
            bgStuffTimeout();
            return;
        }// end function

        protected function adV2End() : void
        {
            return;
        }// end function

        protected function adV2Timeout() : void
        {
            sendTrack(NodeLog.AD_V2_REQUEST_TIMEOUT);
            return;
        }// end function

        protected function adV2Start() : void
        {
            sendTrack(NodeLog.AD_V2_REQUEST_START);
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            var _loc_4:uint = 0;
            var _loc_5:uint = 0;
            var _loc_6:uint = 0;
            var _loc_7:uint = 0;
            var _loc_8:Rectangle = null;
            if (!view)
            {
                return;
            }
            if (param3)
            {
                _loc_8 = PlayerConsts.FULLSCREEN_RECT;
                var _loc_9:* = _loc_8.width;
                _loc_6 = _loc_8.width;
                _loc_4 = _loc_9;
                var _loc_9:* = _loc_8.height;
                _loc_7 = _loc_8.height;
                _loc_5 = _loc_9;
            }
            else
            {
                _loc_4 = PlayerConsts.DEFAULT_VIDEO_WIDTH;
                _loc_5 = PlayerConsts.DEFAULT_VIDEO_HEIGHT;
                _loc_6 = PlayerConsts.STAGE_WIDTH;
                _loc_7 = PlayerConsts.STAGE_HEIGHT;
            }
            view.bgWidth = _loc_6;
            view.bgHeight = _loc_7;
            view.isExpand = PlayerConsts.expandStage;
            view.mainTop = PlayerConsts.VIDEO_TOP;
            view.goTransform(_loc_4, _loc_5, param3);
            return;
        }// end function

        protected function clearV2() : void
        {
            if (v2Loader != null)
            {
                v2Loader.removeEventListener(V2LoaderEvent.COMPLETE, v2LoadedHlr);
                v2Loader.removeEventListener(V2LoaderEvent.IO_ERROR, v2ErrorHlr);
                v2Loader.removeEventListener(V2LoaderEvent.SECURITY_ERROR, v2ErrorHlr);
                v2Loader.removeEventListener(V2LoaderEvent.TIMEOUT_ERROR, v2ErrorHlr);
                v2Loader.close();
            }
            v2Loader = null;
            return;
        }// end function

        protected function initV2() : void
        {
            if (v2Loader)
            {
                clearV2();
            }
            loadedV2 = false;
            v2Loader = new V2AdLoader();
            v2Loader.tryTimes = 1;
            v2Loader.timeout = _loaderV2Time;
            v2Loader.addEventListener(V2LoaderEvent.COMPLETE, v2LoadedHlr);
            v2Loader.addEventListener(V2LoaderEvent.IO_ERROR, v2ErrorHlr);
            v2Loader.addEventListener(V2LoaderEvent.SECURITY_ERROR, v2ErrorHlr);
            v2Loader.addEventListener(V2LoaderEvent.TIMEOUT_ERROR, v2ErrorHlr);
            return;
        }// end function

        protected function v2ErrorHlr(event:V2LoaderEvent) : void
        {
            adItemInfo.videoUrl = "";
            if (event.type == V2LoaderEvent.TIMEOUT_ERROR)
            {
                adV2Timeout();
            }
            else
            {
                adV2Error();
            }
            adV2End();
            return;
        }// end function

        override protected function loadFront() : void
        {
            initFront();
            if (adItemInfo.mtype == AdMaterType.VIDEO)
            {
                loaded = true;
                frontStuffComplete();
            }
            else if (adItemInfo.mtype == AdMaterType.PICTURE)
            {
                startLoadFront(adItemInfo.url);
            }
            return;
        }// end function

        protected function initBg() : void
        {
            if (loaderBg)
            {
                return;
            }
            loadedBg = false;
            loaderBg = new TdLoader();
            loaderBg.loaderContext = new LoaderContext(true, null, SecurityDomain.currentDomain);
            loaderBg.addEventListener(LoaderEvent.COMPLETE, completeBgHlr);
            loaderBg.addEventListener(LoaderEvent.END, endBgHlr);
            loaderBg.addEventListener(LoaderEvent.IO_ERROR, ioErrorBgHlr);
            loaderBg.addEventListener(LoaderEvent.SECURITY_ERROR, ioErrorBgHlr);
            loaderBg.addEventListener(LoaderEvent.TIMEOUT_ERROR, timeoutErrorBgHlr);
            return;
        }// end function

        override protected function clearComps() : void
        {
            clearFront();
            clearBg();
            clearV2();
            return;
        }// end function

        protected function clearBg() : void
        {
            if (loaderBg != null)
            {
                loaderBg.removeEventListener(LoaderEvent.COMPLETE, completeBgHlr);
                loaderBg.removeEventListener(LoaderEvent.END, endBgHlr);
                loaderBg.removeEventListener(LoaderEvent.IO_ERROR, ioErrorBgHlr);
                loaderBg.removeEventListener(LoaderEvent.SECURITY_ERROR, ioErrorBgHlr);
                loaderBg.removeEventListener(LoaderEvent.TIMEOUT_ERROR, timeoutErrorBgHlr);
                loaderBg.close();
            }
            loaderBg = null;
            return;
        }// end function

        protected function startLoadBg(param1:String) : void
        {
            if (loaderBg)
            {
                sendTrack(NodeLog.AD_BG_STUFF_REQUEST_START);
                loaderBg.load(param1);
            }
            return;
        }// end function

        protected function get bgStuff() : DisplayObject
        {
            return loaderBg ? (loaderBg.content) : (null);
        }// end function

        protected function completeBgHlr(event:LoaderEvent) : void
        {
            loadedBg = true;
            sendTrack(NodeLog.AD_BG_STUFF_REQUEST_COMPLETE);
            return;
        }// end function

    }
}

class AdBaseInfo extends BaseInfo
{
    private static const AD_USER_ID:String = "10";

    function AdBaseInfo(param1:int, param2:IAdItemInfo)
    {
        _iid = param1;
        _userId = AD_USER_ID;
        _currRateType = RateType.P256;
        _maxRateType = RateType.P256;
        if (AdType.BACK == param2.type)
        {
            _sourceId = "af";
            _v2pr = "1";
            if (param2.hd == 1)
            {
                _currRateType = RateType.P360;
                _maxRateType = RateType.P360;
            }
        }
        else
        {
            _sourceId = "";
        }
        return;
    }// end function

}

